package org.xserver.component.core.dispatch;

import java.util.HashMap;
import java.util.Map;

import org.xserver.component.core.XServerHttpContextAttachment;
import org.xserver.component.core.dispatch.http.HttpRequestDispatchFactory.HttpRequestType;

/**
 * The manager of {@link RequestDispatch}, it also is a factory of
 * {@link RequestDispatch}.
 * 
 * @author idoldefine
 * @since 2016/07/18
 *
 */
public class RequestDispatchManager {
	/**
	 * the request dispatch map, it configure in classpath:interfaceContext.xml
	 */
	private Map<String, RequestDispatch> requestDispatchInfos = new HashMap<String, RequestDispatch>(
			4);

	public Map<String, RequestDispatch> getRequestDispatchInfos() {
		return requestDispatchInfos;
	}

	public void setRequestDispatchInfos(
			Map<String, RequestDispatch> requestDispatchInfos) {
		this.requestDispatchInfos = requestDispatchInfos;
	}

	/**
	 * Factory method get instance
	 * 
	 * @param attachment
	 * @return
	 */
	public RequestDispatch getInstance(XServerHttpContextAttachment attachment) {
		String key = HttpRequestType.INTERFACE.getType();

		if (attachment.getRequest().getPath().contains(".")) {
			key = HttpRequestType.FILE.getType();
		}
		if(attachment.isWebsocket()){
			key = HttpRequestType.WEBSOCKET.getType();
		}

		return requestDispatchInfos.get(key);
	}
}
